package com.qst.financing.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.financing.dao.FinanceDao;
import com.qst.financing.entity.Finance;
import com.qst.financing.entity.Recommender;
import com.qst.financing.service.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FinanceServiceImpl implements FinanceService {
    @Autowired
    private FinanceDao financeDao;

    @Override
    public PageInfo<Finance> selectByFinance(Finance finance,Integer pageNum) {
        PageHelper.startPage(pageNum,20);
        List<Finance> finances = financeDao.selectByFinance(finance);
        for(Finance f: finances){
            String combinationName = "";
            if(f.getCombinationName1()==null&&f.getCombinationName2()==null){
                combinationName += "无";
            }
            if(f.getCombinationName1()!=null){
                combinationName += f.getCombinationName1();
            }
            if(f.getCombinationName2()!=null){
                combinationName += "、" + f.getCombinationName2();
            }
            f.setCombinationName(combinationName);
        }
        PageInfo<Finance> financePageInfo = new PageInfo<>(finances);
        return financePageInfo;
    }

    @Override
    public Finance selectByFinanceId(Integer id){

        Finance finance = financeDao.selectByPrimaryKey(id);
        if(finance!=null && finance.getFileInfo()!=null){
            String[] s = finance.getFileInfo().split(";");
            finance.setFile(s);
        }
        return finance;
    }

    @Override
    public void updateByFinance(Finance finance){

        financeDao.updateByPrimaryKeySelective(finance);
    }

    @Override
    public PageInfo<Recommender> selectRecommend(Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<Recommender> orderRecommenders = financeDao.selectRecommendFromOrder();
        PageInfo<Recommender> pageInfo = new PageInfo<>(orderRecommenders);
//        System.out.println(pageInfo);
        return pageInfo;
    }

    @Override
    public String selectBankName(Integer bankid) {
        return financeDao.selectBankName(bankid);
    }

    @Override
    public List<String> getPic(Integer id) {
        System.out.println("融资人："+id);
        String pic = financeDao.getPic(id);

        String[] strings = pic.split(" ");
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < strings.length; i++) {
            list.add(strings[i]);
        }
        System.out.println(list);
        return list;
    }

}
